
--------------------------------------------------

local nStartTime = CurTime()

MsgN('Loading "ds_menu/vgui_startnewgame.lua"')
local sFile = 'ds_menu/vgui_startnewgame.lua'

--------------------------------------------------

local w , h = ( 640 ) , ( 480 )
local x , y = ( ( w / 2 ) - ( w / 2 ) ) , ( ( h / 2 ) - ( h / 2 ) )

if (!ConVarExists( 'cl_menu_sng_switch' )) then CreateClientConVar( 'cl_menu_sng_switch' , tostring( 0 ) , true , false ) end
if (!ConVarExists( 'cl_menu_sng_x' )) then CreateClientConVar( 'cl_menu_sng_x' , tostring( x ) , true , false ) end
if (!ConVarExists( 'cl_menu_sng_y' )) then CreateClientConVar( 'cl_menu_sng_y' , tostring( y ) , true , false ) end
// if (!ConVarExists( 'cl_menu_sng_n' )) then CreateClientConVar( 'cl_menu_sng_n' , tostring( 5 ) , true , false ) end

local bw , bh = ( 122 ) , ( 122 )

--------------------------------------------------

local function getCvarPanel( sCvar , tData , dParent , bOff , nWideAdjust )
	local cvar_vgui = vgui.Create( "DPanel" , dParent )
	cvar_vgui:SetSize( dParent:GetWide() + (nWideAdjust or 0) , 22 )
	cvar_vgui.Paint = function()
		if (bOff) then
			draw.RoundedBox( 3, 0, 0, cvar_vgui:GetWide() , cvar_vgui:GetTall(), Color( 80, 80, 80, 102 ) )
		end
	end
	
	local correct = 4 + 16
	
	if (tData.type == 'header') then
		cvar_vgui.dHeader = vgui.Create( "DPanel" , cvar_vgui )
		cvar_vgui.dHeader:SetSize( dParent:GetWide() , 22 )
		cvar_vgui.dHeader.Paint = function()
			draw.RoundedBox( 3, 0, 0, cvar_vgui.dHeader:GetWide() , cvar_vgui.dHeader:GetTall(), Color( 80, 80, 80, 250 ) )
		end
	elseif (tData.type == 'numslider' or tData.type == 'numwang') then
		if (tData.value) then RunConsoleCommand( sCvar or '' , tData.value ) end
		local NumSlider = vgui.Create( "DNumberWang" , cvar_vgui )
		NumSlider:SetPos( cvar_vgui:GetWide() / 3 , 0 )
		NumSlider:SetSize( ( ( cvar_vgui:GetWide() / 3 ) * 2 ) - correct , cvar_vgui:GetTall() )
		NumSlider:SetText( "Max Props" )
		NumSlider:SetMin( tData.min or 0 )
		NumSlider:SetMax( tData.max or 9999 )
		NumSlider:SetDecimals( tData.dec or 0 )
		NumSlider:SetConVar( sCvar or '' )
		NumSlider:SetValue( GetConVarNumber( sCvar or '' ) )
	elseif (tData.type == 'checkbox') then
		if (tData.value) then RunConsoleCommand( sCvar or '' , tData.value ) end
		local CheckBox = vgui.Create( "DCheckBoxLabel", cvar_vgui )
		CheckBox:SetPos( cvar_vgui:GetWide() / 3 , 4 )
		CheckBox:SetSize( ( ( cvar_vgui:GetWide() / 3 ) * 2 ) - correct , cvar_vgui:GetTall() - 8 )
		CheckBox:SetText( 'Check / Uncheck' .. string.rep( ' ' , 280 ) )
		CheckBox:SetConVar( sCvar or '' )
		CheckBox:SetValue( GetConVarNumber( sCvar or '' ) )
	elseif (tData.type == 'dropdown') then
		if (tData.value) then RunConsoleCommand( sCvar or '' , tData.value ) end
		local MultiChoice = vgui.Create( "DMultiChoice", cvar_vgui )
		MultiChoice:SetPos( cvar_vgui:GetWide() / 3 , 0)
		MultiChoice:SetSize( ( ( cvar_vgui:GetWide() / 3 ) * 2 ) - correct , cvar_vgui:GetTall() )
		MultiChoice.Options = {}
		
		local id = 1
		for k,v in pairs(tData.data) do
			MultiChoice.Options[id] = {Key = k , Val = v}
			MultiChoice:AddChoice( k )
			if (tostring( v ) == GetConVarString( sCvar )) then
				MultiChoice:ChooseOptionID( id )
			end
			id = ( id + 1 )
		end
		
		MultiChoice.OnSelect = function(pan,index,value,data)
			local tSelected = MultiChoice.Options[index]
			RunConsoleCommand( sCvar , tSelected.Val )
		end
	elseif (tData.type == 'textbox') then
		if (tData.value) then RunConsoleCommand( sCvar or '' , tData.value ) end
		local TextBox = vgui.Create("DTextEntry", cvar_vgui)
		TextBox:SetMultiline( false )
		TextBox:SetPos( cvar_vgui:GetWide() / 3 , 0 )
		TextBox:SetSize( ( ( cvar_vgui:GetWide() / 3 ) * 2 ) - correct , cvar_vgui:GetTall() )
		TextBox:SetConVar( sCvar or '' )
		TextBox:SetValue( GetConVarNumber( sCvar or '' ) )
		TextBox:SetEditable( true )
		TextBox:SetUpdateOnType( true )
	end
	
	cvar_vgui.Label = vgui.Create("DLabel", cvar_vgui)
	cvar_vgui.Label:SetPos(4,4)
	cvar_vgui.Label:SetColor(Color(255,255,255,255))
	cvar_vgui.Label:SetFont("default")
	cvar_vgui.Label:SetText(tData.label)
	cvar_vgui.Label:SetSize(dParent:GetWide() / 3 , 14)
	
	return cvar_vgui
end

local texture_start = surface.GetTextureID( "gui/silkicons/star" )
local function getMapIcon( sMap , dParent , self )
	local mapMenu = {
		{	Text = "Add to Favorites",
			Check = function( sMap , dMap )
				return (!g_ds_menu.func_IsFavoriteMap( sMap ))
			end,
			Call = function( sMap , dMap )
				self:AddFav( sMap )
			end,
		},
		{	Text = "Remove from Favorites",
			Check = function( sMap , dMap )
				return (g_ds_menu.func_IsFavoriteMap( sMap ))
			end,
			Call = function( sMap , dMap )
				self:RemoveFav( sMap )
			end,
		},
	}
	
	local MapIcon = vgui.Create( "MapIcon" , dParent )
	MapIcon:SetMap({Name = ( sMap ) , Material = ( 'maps/' .. sMap .. '.vmt' )})
	MapIcon:SetSize( bw , bh )
	MapIcon.PerformLayout = function()
		MapIcon:SetSize( bw, bh )
		
		MapIcon.Label:SetPos( 10, MapIcon:GetTall() - MapIcon.Label:GetTall() )
		MapIcon.Label:SetWide( MapIcon:GetWide() - 20 )
		MapIcon.Label:SetContentAlignment( 5 )
		MapIcon.Label:SetZPos( 100 )
		
		local size = MapIcon:GetTall() - MapIcon.Label:GetTall()
		MapIcon.Icon:SetSize( MapIcon:GetWide()-2, MapIcon:GetTall()-2 )
		MapIcon.Icon:SetPos( 1, 1 )
	end
	MapIcon.PaintOver = function()
		if ( g_SelectedMap == MapIcon.MapName ) then
			surface.SetDrawColor( 255, 155, 20, 255 )
		else
			surface.SetDrawColor( 155, 155, 155, 200 )
		end
		
		local x, y = MapIcon.Label:GetPos()
		
		surface.DrawRect( 1, y, MapIcon:GetWide() - 2, MapIcon:GetTall() - y )
		
		MapIcon.Label:PaintManual()
		
		if (g_ds_menu.func_IsFavoriteMap( sMap )) then
			surface.SetTexture( texture_start )
			surface.SetDrawColor( 255 , 255 , 255 , 255 )
			surface.DrawTexturedRect( bw - 16 - 1 , 1 , 16 , 16 )
		end
	end
	MapIcon.OnMousePressed = function( panel , mcMouse )
		if (mcMouse == MOUSE_LEFT) then MapIcon.DoClick() end
		if (mcMouse == MOUSE_RIGHT) then MapIcon.DoRightClick() end
	end
	MapIcon.DoClick = function()
		self:SetMap( sMap )
		self.Map = sMap
		g_SelectedMap = MapIcon
	end
	MapIcon.DoRightClick = function()
		MapIcon.DoClick()
		
		local dMenu = DermaMenu()
		local nMenu = 0
		
		for nIndex , tData in pairs( mapMenu ) do
			if (tData.Check && tData.Check( sMap , MapIcon )) then
				nMenu = nMenu + 1
				dMenu:AddOption( tData.Text or "<error>" , function() tData.Call( sMap , MapIcon ) end )
			elseif (tData.SubM) then
				local dSubMenu = dMenu:AddSubMenu(tData.Text)
				
				for nIndex2 , tData2 in pairs( tData.SubM ) do
					if (tData2.Check( sMap , MapIcon )) then
						nMenu = nMenu + 1
						dSubMenu:AddOption( tData2.Text or "<error>" , function() tData2.Call( sMap , MapIcon ) end )
					end
				end
			end
		end
		
		if (nMenu < 1) then
			dMenu:Remove()
		else
			dMenu:Open()
		end
	end
	
	return MapIcon
end

--------------------------------------------------

local PANEL = {}

function PANEL:AddFav( sMap )
	g_ds_menu.func_AddFavoriteMap( sMap )
	
	timer.Simple( 0.2 , function() g_ds_menu.func_SaveFavoriteMaps() end )
	timer.Simple( 0.4 , function() self:PopulateFavMaps() end )
end

function PANEL:RemoveFav( sMap )
	g_ds_menu.func_RemoveFavoriteMap( sMap )
	
	timer.Simple( 0.2 , function() g_ds_menu.func_SaveFavoriteMaps() end )
	timer.Simple( 0.4 , function() self:PopulateFavMaps() end )
end

function PANEL:PopulateFavMaps()
	if (!self.TabSheet.MapsT) then return end
	
	local tMaps = g_ds_menu.func_GetFavoriteMaps()
	
	local bAdd = false
	
	local bn = 5
	local nLines = ( math.ceil( #tMaps / bn ) )
	
	local function func_Populate()
		local bn = 5
		local tMaps = g_ds_menu.func_GetFavoriteMaps()
		local nLines = ( math.ceil( #tMaps / bn ) )
		self.TabSheet.MapsT.Category["Favorites"].List:Clear()
		
		for nIndex , sMap in pairs( tMaps ) do
			local MapIcon = getMapIcon( sMap , self.TabSheet.MapsT.Category["Favorites"].List , self )
			
			self.TabSheet.MapsT.Category["Favorites"].List:AddItem( MapIcon )
		end
		
		self.TabSheet.MapsT.Category["Favorites"].List:SetTall( ( ( bh + 2 ) * math.ceil( nLines ) ) )
		
		return ( ( bh + 2 ) * math.ceil( nLines ) )
	end
	
	if (!self.TabSheet.MapsT.Category["Favorites"]) then bAdd = true self.TabSheet.MapsT.Category["Favorites"] = vgui.Create("DCollapsibleCategory", self.TabSheet.MapsT) end
	self.TabSheet.MapsT.Category["Favorites"]:SetExpanded( 0 )
	self.TabSheet.MapsT.Category["Favorites"]:SetTall( 22 )
	self.TabSheet.MapsT.Category["Favorites"]:SetLabel( 'Favorites' .. ' ( ' .. ( #tMaps ) .. ' , ' .. nLines .. ' )' )
	self.TabSheet.MapsT.Category["Favorites"].OnMousePressed = function( panel , mcMouse )
		if (mcMouse == MOUSE_RIGHT) then
			local tall = func_Populate()
			self.TabSheet.MapsT.Category["Favorites"].List:SetTall( tall )
			self.TabSheet.MapsT.Category["Favorites"]:Toggle()
			self.TabSheet.MapsT.Category["Favorites"]:Toggle()
		end
	end
	if (!self.TabSheet.MapsT.Category["Favorites"].List) then self.TabSheet.MapsT.Category["Favorites"].List = vgui.Create("DPanelList", self.TabSheet.MapsT.Category["Favorites"]) end
	self.TabSheet.MapsT.Category["Favorites"].List:SetTall( 500 )
	self.TabSheet.MapsT.Category["Favorites"].List:EnableHorizontal( true )
	self.TabSheet.MapsT.Category["Favorites"].List:EnableVerticalScrollbar( false )
	self.TabSheet.MapsT.Category["Favorites"].List:SetSpacing( 2 )
	self.TabSheet.MapsT.Category["Favorites"].List.Maps = {}
	self.TabSheet.MapsT.Category["Favorites"].List:Clear()
	
	local tall = func_Populate()
	self.TabSheet.MapsT.Category["Favorites"].List:SetTall( tall )
	
	if (bAdd == true) then
		self.TabSheet.MapsT.Category["Favorites"]:SetContents( self.TabSheet.MapsT.Category["Favorites"].List )
		
		self.TabSheet.MapsT:AddItem( self.TabSheet.MapsT.Category["Favorites"] )
	end
	
	self.TabSheet.MapsT.Category["Favorites"]:Toggle()
	self.TabSheet.MapsT.Category["Favorites"]:Toggle()
	
	// self.TabSheet.MapsT:AddItem( self.TabSheet.MapsT.Category[sCategory] )
end

function PANEL:PopulateMaps( sMapFilter , sCategoryFilter )
	MsgN( 'Search: ' , sMapFilter , ' / ' , sCategoryFilter )
	local nPopulateStartTime = CurTime()
	
	if (!self.TabSheet.MapsT) or (!self.TabSheet.MapsL) then return end
	
	self.TabSheet.MapsT.Empty()
	self.TabSheet.MapsL.Empty()
	
	self:PopulateFavMaps()
	
	for nIndex , sCategory in pairs( g_ds_menu.tMapsIndex ) do
		local tMaps = table.Copy( g_ds_menu.tMaps[ sCategory ] or {} )
		local tMapsFiltered = {}
		
		if ( sCategoryFilter ) && ( string.len( sCategoryFilter ) > 0) then
			if ( !g_ds_menu.check( sCategory , sCategoryFilter ) ) then
				tMaps = {}
			end
		end
		
		if ( sMapFilter ) && ( string.len( sMapFilter ) > 0) then
			for nIndex , sMap in pairs( tMaps ) do
				if (g_ds_menu.checkMap( sMap , sMapFilter )) then
					table.insert( tMapsFiltered , sMap )
				end
			end
			tMaps = tMapsFiltered
		end
		
		if (#tMaps > 0) then
			local bn = 5
			local nLines = ( math.ceil( #tMaps / bn ) )
			
			if (!self.TabSheet.MapsT.Category[sCategory]) then self.TabSheet.MapsT.Category[sCategory] = vgui.Create("DCollapsibleCategory", self.TabSheet.MapsT) end
			self.TabSheet.MapsT.Category[sCategory]:SetTall( 22 )
			self.TabSheet.MapsT.Category[sCategory]:SetExpanded( 0 )
			self.TabSheet.MapsT.Category[sCategory]:SetLabel( sCategory .. ' ( ' .. ( #tMaps ) .. ' , ' .. nLines .. ' )' )
			
			if (!self.TabSheet.MapsT.Category[sCategory].List) then self.TabSheet.MapsT.Category[sCategory].List = vgui.Create("DPanelList", self.TabSheet.MapsT.Category[sCategory]) end
			self.TabSheet.MapsT.Category[sCategory].List:SetTall( 0 )
			self.TabSheet.MapsT.Category[sCategory].List:EnableHorizontal( true )
			self.TabSheet.MapsT.Category[sCategory].List:EnableVerticalScrollbar( false )
			self.TabSheet.MapsT.Category[sCategory].List:SetSpacing( 2 )
			self.TabSheet.MapsT.Category[sCategory].List.Maps = {}
			
			local bw , bh = ( 122 ) , ( 122 )
			
			for nIndex , sMap in pairs( tMaps ) do
				if (!table.HasValue(self.TabSheet.MapsT.Category[sCategory].List.Maps , sMap)) then
					local MapIcon = getMapIcon( sMap , self.TabSheet.MapsT.Category[sCategory].List , self )
					
					self.TabSheet.MapsT.Category[sCategory].List:AddItem( MapIcon )
					
					table.insert(self.TabSheet.MapsT.Category[sCategory].List.Maps , sMap)
				end
				if (!table.HasValue(self.TabSheet.MapsL.Maps , sMap)) then self.TabSheet.MapsL:AddLine( sMap ) table.insert( self.TabSheet.MapsL.Maps , sMap ) end
			end
			self.TabSheet.MapsT.Category[sCategory].List:SetTall( ( ( bh + 2 ) * math.ceil( nLines ) ) )
			
			self.TabSheet.MapsT.Category[sCategory]:SetContents( self.TabSheet.MapsT.Category[sCategory].List )
			
			self.TabSheet.MapsT:AddItem( self.TabSheet.MapsT.Category[sCategory] )
		end
		
	end
	
	self.TabSheet.MapsL:SortByColumn( 1, false )
	
	MsgN( 'Search: ' , sMapFilter or '<empty filter>' , ' of ' , #self.TabSheet.MapsL.Maps , ' results took ' , ( math.Round( ( CurTime() - nPopulateStartTime ) * 10000 ) / 10000 ) , ' seconds.' )
end

function PANEL:Recover()
	local nX , nY = self:GetPos()
	
	if (nX < 0) or (nX > ( ScrW() - w )) or (nY < 0) or (nY > ( ScrH() - h )) then
		self:SetPos( math.Clamp( nX , 0 , ( ScrW() - w ) ) , math.Clamp( nY , 0 , ( ScrH() - h ) ) )
	end
end

function PANEL:Close()
	local nX , nY = self:GetPos()
	
	RunConsoleCommand( 'cl_menu_sng_x' , tostring( nX ) )
	RunConsoleCommand( 'cl_menu_sng_y' , tostring( nX ) )
	
	self:SetVisible( false )
end

function PANEL:Init()
	self:SetTitle( 'Start New Game' )
	self:SetMouseInputEnabled( true )
	self:SetKeyboardInputEnabled( true )
	
	self:SetPos( GetConVarNumber( 'cl_menu_sng_x' ) , GetConVarNumber( 'cl_menu_sng_y' ) )
	self:SetSize( w , h )
	
	self:ShowCloseButton( true )
	self:SetDeleteOnClose( false )
	
	self:SetSizable( false )
	self:SetDraggable( true )
	
	self:SetVisible( false )
	
	if (GetConVarNumber( 'cl_menu_sng_switch' ) == 1) then
		self:Center()
	end
	
	self.Map = nil
	
	self:MakePopup()
	
	timer.Create( 'vgui_startnewgame_title' , 1 , 0 , function()
		local sGamemode = GetConVarString( 'sv_gamemodeoverride' )
		local sMap = self.Map or '<none>'
		
		self:SetTitle( 'Start New Game (' .. sGamemode .. ' , ' .. sMap .. ')'  )
	end )
	
--------------------------------------------------
	
	if (!self.ButtonStartSPGame) then self.ButtonStartSPGame = vgui.Create( "DButton" , self ) end
	self.ButtonStartSPGame:SetText( 'Start Single-Player' )
	self.ButtonStartSPGame:SetPos( self:GetWide() - 102 , self:GetTall() - 25 )
	self.ButtonStartSPGame:SetSize( 100 , 22 )
	self.ButtonStartSPGame:SetDisabled( true )
	self.ButtonStartSPGame.DoClick = function()
		if (!self.Map) then return end
		hook.Call( "StartGame", {} )
		
		RunConsoleCommand( "progress_enable" )
		RunConsoleCommand( "disconnect" )
		RunConsoleCommand( "maxplayers", 1 )
		
		timer.Simple( 0.2, function() RunConsoleCommand( "map", self.Map ) end )
		
		cookie.Set( "menuui.hostname", GetConVarString( "hostname" ) )
	end
	
--------------------------------------------------
	
	if (!self.ButtonStartMPGame) then self.ButtonStartMPGame = vgui.Create( "DButton" , self ) end
	self.ButtonStartMPGame:SetText( 'Start Multi-Player' )
	self.ButtonStartMPGame:SetPos( 2 , self:GetTall() - 25 )
	self.ButtonStartMPGame:SetSize( 100 , 22 )
	self.ButtonStartMPGame:SetDisabled( true )
	self.ButtonStartMPGame.DoClick = function()
		if (!self.Map) then return end
		hook.Call( "StartGame", {} )
		
		RunConsoleCommand( "progress_enable" )
		RunConsoleCommand( "disconnect" )
		RunConsoleCommand( "sv_cheats", "0" )
		
		timer.Simple( 0.1, function() RunConsoleCommand( "maxplayers", GetConVarNumber( "sv_maxplayers" ) ) end )
		
		timer.Simple( 0.2, function() RunConsoleCommand( "map", self.Map ) end )
		
		cookie.Set( "menuui.hostname", GetConVarString( "hostname" ) )
	end
	
--------------------------------------------------
	
	if (!self.ButtonMapsReload) then self.ButtonMapsReload = vgui.Create( "DButton" , self ) end
	self.ButtonMapsReload:SetText( '' )
	self.ButtonMapsReload:SetPos( self:GetWide() - 2 - 22 , 23 )
	self.ButtonMapsReload:SetSize( 22 , 22 )
	self.ButtonMapsReload:SetDisabled( false )
	self.ButtonMapsReload:SetImage( 'gui/silkicons/arrow_refresh' )
	self.ButtonMapsReload.DoClick = function()
		local sMapFilter = (self.TextEntryMapsSearch.Clicked and self.TextEntryMapsSearch:GetValue() or nil)
		local sCategoryFilter = (self.TextEntryMapsCategory.Clicked and self.TextEntryMapsCategory:GetValue() or nil)
		
		if (string.len( sMapFilter or '' ) < 1) then sMapFilter = nil end
		if (string.len( sCategoryFilter or '' ) < 1) then sCategoryFilter = nil end
		
		self:PopulateMaps( sMapFilter , sCategoryFilter )
	end
	
	if (!self.TextEntryMapsSearch) then self.TextEntryMapsSearch = vgui.Create( "DTextEntry" , self ) end
	self.TextEntryMapsSearch:SetPos( self:GetWide() - 2 - 22 - 250 , 23 )
	self.TextEntryMapsSearch:SetSize( 126 , 22 )
	self.TextEntryMapsSearch:SetText( '<Map Filter>' )
	self.TextEntryMapsSearch.OnGetFocus = function()
		if (!self.TextEntryMapsSearch.Clicked) then
			self.TextEntryMapsSearch.Clicked = true
			self.TextEntryMapsSearch:SetText( '' )
		end
	end
	self.TextEntryMapsSearch.OnEnter = function()
		self.ButtonMapsReload.DoClick()
	end
	self.TextEntryMapsSearch.Clicked = false
	
	if (!self.TextEntryMapsCategory) then self.TextEntryMapsCategory = vgui.Create( "DTextEntry" , self ) end
	self.TextEntryMapsCategory:SetPos( self:GetWide() - 2 - 22 - 125 , 23 )
	self.TextEntryMapsCategory:SetSize( 126 , 22 )
	self.TextEntryMapsCategory:SetText( '<Category Filter>' )
	self.TextEntryMapsCategory.OnGetFocus = function()
		if (!self.TextEntryMapsCategory.Clicked) then
			self.TextEntryMapsCategory.Clicked = true
			self.TextEntryMapsCategory:SetText( '' )
		end
	end
	self.TextEntryMapsCategory.OnEnter = function()
		self.ButtonMapsReload.DoClick()
	end
	self.TextEntryMapsCategory.Clicked = false
	
	if (!self.LabelMapsSrearch) then self.LabelMapsSrearch = vgui.Create( "DLabel" , self ) end
	self.LabelMapsSrearch:SetPos( self:GetWide() - 2 - 22 - 250 - 38 , 23 )
	self.LabelMapsSrearch:SetSize( 250 + 38 , 22 )
	self.LabelMapsSrearch:SetText( 'Search:' )
	
--------------------------------------------------
	
	/*
		Maps (Thumbnails) / categorized thumbnails
			
		Maps (List) / very basic list of maps
			
		Settings / 
			Basic
			Advanced
			Addons
			Gamemode
		Map Information
			shows information about map including data gathered last time map was loaded
	*/
	
	if (!self.TabSheet) then self.TabSheet = vgui.Create( "DPropertySheet", self ) end
	self.TabSheet:SetPadding( 1 )
	self.TabSheet:SetPos( 2, 22 + 22 + 2 )
	self.TabSheet:SetSize( self:GetWide() - 4, self:GetTall() - 24 - 26 - 22 )
	
--------------------------------------------------
	
	if (!self.TabSheet.MapsT) then self.TabSheet.MapsT = vgui.Create( "DPanelList", self.TabSheet ) end
	self.TabSheet.MapsT:SetPos( 0,0 )
	self.TabSheet.MapsT:SetSize( self.TabSheet:GetWide(), self.TabSheet:GetTall() )
	self.TabSheet.MapsT:SetSpacing( 2 )
	self.TabSheet.MapsT:EnableHorizontal( false )
	self.TabSheet.MapsT:EnableVerticalScrollbar( true )
	self.TabSheet.MapsT.Category = {}
	
	self.TabSheet.MapsT.Empty = function()
		for sFileName , dPanelList in pairs( self.TabSheet.MapsT.Category ) do
			dPanelList:Clear()
			dPanelList:Remove()
		end
		self.TabSheet.MapsT.Category = {}
		self.TabSheet.MapsT:Clear()
		
	end
	
	if (!self.TabSheet.MapsL) then self.TabSheet.MapsL = vgui.Create( "DListView", self.TabSheet ) end
	self.TabSheet.MapsL:SetPos( 0,0 )
	self.TabSheet.MapsL:SetSize( self.TabSheet.MapsL:GetWide(), self.TabSheet.MapsL:GetTall() )
	self.TabSheet.MapsL.Column_Name = self.TabSheet.MapsL:AddColumn('Name')
	// self.TabSheet.MapsL.Column_Name:SetWide( self.TabSheet.MapsL:GetWide() * 0.35 )
	// self.TabSheet.MapsL.Column_Categories = self.TabSheet.MapsL:AddColumn('Categories')
	// self.TabSheet.MapsL.Column_Categories:SetWide( self.TabSheet.MapsL:GetWide() / 0.35 )
	self.TabSheet.MapsL.OnRowSelected = function(panel , line)
		self:SetMap( self.TabSheet.MapsL:GetLine(line):GetValue(1) )
	end
	self.TabSheet.MapsL.Maps = {}
	
	self.TabSheet.MapsL.Empty = function()
		self.TabSheet.MapsL.Maps = {}
		self.TabSheet.MapsL:Clear()
	end
	
	self:PopulateMaps()
	
--------------------------------------------------
	
	/*
		Basic
		Advanced
		Addons
		Gamemode
	*/
	
	if (!self.TabSheet.Settings) then self.TabSheet.Settings = vgui.Create( "DPropertySheet", self.TabSheet ) end
	self.TabSheet.Settings:SetPadding( 1 )
	self.TabSheet.Settings:SetPos( 0,0 )
	self.TabSheet.Settings:SetSize( self.TabSheet:GetWide(), self.TabSheet:GetTall() )
	
--------------------------------------------------
	
	if (!self.TabSheet.Settings.Basic) then self.TabSheet.Settings.Basic = vgui.Create( "DPanelList", self.TabSheet.Settings ) end
	self.TabSheet.Settings.Basic:SetPos( 0,0 )
	self.TabSheet.Settings.Basic:SetSize( self.TabSheet:GetWide(), self.TabSheet:GetTall() )
	self.TabSheet.Settings.Basic:SetSpacing( 1 )
	self.TabSheet.Settings.Basic:EnableHorizontal( false )
	self.TabSheet.Settings.Basic:EnableVerticalScrollbar( true )
	self.TabSheet.Settings.Basic.Groups = {}
	
	for sFileName , tCvar in pairs( g_ds_menu.tSettings.Basic ) do
		if (!self.TabSheet.Settings.Basic.Groups[sFileName]) then self.TabSheet.Settings.Basic.Groups[sFileName] = vgui.Create("DPanelList" , self.TabSheet.Settings.Basic) end
		self.TabSheet.Settings.Basic.Groups[sFileName]:SetWide( self.TabSheet.Settings.Basic:GetWide() )
		self.TabSheet.Settings.Basic.Groups[sFileName]:SetTall( 22 )
		self.TabSheet.Settings.Basic.Groups[sFileName]:SetSpacing( 0 )
		self.TabSheet.Settings.Basic.Groups[sFileName]:EnableHorizontal( false )
		self.TabSheet.Settings.Basic.Groups[sFileName]:EnableVerticalScrollbar( true )
		self.TabSheet.Settings.Basic.Groups[sFileName].Paint = function()
			draw.RoundedBox( 3, 0, 0, self.TabSheet.Settings.Basic.Groups[sFileName]:GetWide() , self.TabSheet.Settings.Basic.Groups[sFileName]:GetTall(), Color( 102, 102, 102, 250 ) )
		end
		
		self.TabSheet.Settings.Basic.Groups[sFileName].Cvars = {}
		
		self.TabSheet.Settings.Basic.Groups[sFileName]:AddItem( getCvarPanel( 'dHeader' , {
			['type'] = 'header',
			['label'] = sFileName,
		} , self.TabSheet.Settings.Basic.Groups[sFileName] ) )
		
		local bOff = false
		for nIndex , tData in pairs( tCvar ) do
			local cvar_vgui = getCvarPanel( tData.cvar , tData , self.TabSheet.Settings.Basic.Groups[sFileName] , bOff )
			self.TabSheet.Settings.Basic.Groups[sFileName].Cvars[nIndex] = cvar_vgui
			self.TabSheet.Settings.Basic.Groups[sFileName]:AddItem( cvar_vgui )
			self.TabSheet.Settings.Basic.Groups[sFileName]:SetTall( self.TabSheet.Settings.Basic.Groups[sFileName]:GetTall() + cvar_vgui:GetTall() )
			bOff = !bOff
		end
		
		self.TabSheet.Settings.Basic:AddItem( self.TabSheet.Settings.Basic.Groups[sFileName] )
	end
	
--------------------------------------------------
	
	if (!self.TabSheet.Settings.Advanced) then self.TabSheet.Settings.Advanced = vgui.Create( "DPanelList", self.TabSheet.Settings ) end
	self.TabSheet.Settings.Advanced:SetPos( 0,0 )
	self.TabSheet.Settings.Advanced:SetSize( self.TabSheet:GetWide(), self.TabSheet:GetTall() )
	self.TabSheet.Settings.Advanced:SetSpacing( 1 )
	self.TabSheet.Settings.Advanced:EnableHorizontal( false )
	self.TabSheet.Settings.Advanced:EnableVerticalScrollbar( true )
	self.TabSheet.Settings.Advanced.Groups = {}
	
	for sFileName , tCvar in pairs( g_ds_menu.tSettings.Advanced ) do
		if (!self.TabSheet.Settings.Advanced.Groups[sFileName]) then self.TabSheet.Settings.Advanced.Groups[sFileName] = vgui.Create("DPanelList" , self.TabSheet.Settings.Advanced) end
		self.TabSheet.Settings.Advanced.Groups[sFileName]:SetWide( self.TabSheet.Settings.Advanced:GetWide() )
		self.TabSheet.Settings.Advanced.Groups[sFileName]:SetTall( 22 )
		self.TabSheet.Settings.Advanced.Groups[sFileName]:SetSpacing( 0 )
		self.TabSheet.Settings.Advanced.Groups[sFileName]:EnableHorizontal( false )
		self.TabSheet.Settings.Advanced.Groups[sFileName]:EnableVerticalScrollbar( true )
		self.TabSheet.Settings.Advanced.Groups[sFileName].Paint = function()
			draw.RoundedBox( 3, 0, 0, self.TabSheet.Settings.Advanced.Groups[sFileName]:GetWide() , self.TabSheet.Settings.Advanced.Groups[sFileName]:GetTall(), Color( 102, 102, 102, 250 ) )
		end
		
		self.TabSheet.Settings.Advanced.Groups[sFileName].Cvars = {}
		
		self.TabSheet.Settings.Advanced.Groups[sFileName]:AddItem( getCvarPanel( 'dHeader' , {
			['type'] = 'header',
			['label'] = sFileName,
		} , self.TabSheet.Settings.Advanced.Groups[sFileName] ) )
		
		local bOff = false
		for nIndex , tData in pairs( tCvar ) do
			local cvar_vgui = getCvarPanel( tData.cvar , tData , self.TabSheet.Settings.Advanced.Groups[sFileName] , bOff )
			self.TabSheet.Settings.Advanced.Groups[sFileName].Cvars[nIndex] = cvar_vgui
			self.TabSheet.Settings.Advanced.Groups[sFileName]:AddItem( cvar_vgui )
			self.TabSheet.Settings.Advanced.Groups[sFileName]:SetTall( self.TabSheet.Settings.Advanced.Groups[sFileName]:GetTall() + cvar_vgui:GetTall() )
			bOff = !bOff
		end
		
		self.TabSheet.Settings.Advanced:AddItem( self.TabSheet.Settings.Advanced.Groups[sFileName] )
	end
	
--------------------------------------------------
	
	if (!self.TabSheet.Settings.Addons) then self.TabSheet.Settings.Addons = vgui.Create( "DPanelList", self.TabSheet.Settings ) end
	self.TabSheet.Settings.Addons:SetPos( 0,0 )
	self.TabSheet.Settings.Addons:SetSize( self.TabSheet:GetWide(), self.TabSheet:GetTall() )
	self.TabSheet.Settings.Addons:SetSpacing( 1 )
	self.TabSheet.Settings.Addons:EnableHorizontal( false )
	self.TabSheet.Settings.Addons:EnableVerticalScrollbar( true )
	self.TabSheet.Settings.Addons.Groups = {}
	
--------------------------------------------------
	
	if (!self.TabSheet.Settings.Gamemode) then self.TabSheet.Settings.Gamemode = vgui.Create( "DPanelList", self.TabSheet.Settings ) end
	self.TabSheet.Settings.Gamemode:SetPos( 0,0 )
	self.TabSheet.Settings.Gamemode:SetSize( self.TabSheet:GetWide(), self.TabSheet:GetTall() )
	self.TabSheet.Settings.Gamemode:SetSpacing( 1 )
	self.TabSheet.Settings.Gamemode:EnableHorizontal( false )
	self.TabSheet.Settings.Gamemode:EnableVerticalScrollbar( true )
	self.TabSheet.Settings.Gamemode.GameFolder = ''
	self.TabSheet.Settings.Gamemode.Groups = {}
	
	timer.Create( 'vgui_startnewgame_settings_gamemode' , 0.2 , 0 , function() if (self.TabSheet.Settings.Gamemode.GameFolder != GetConVarString('sv_gamemodeoverride') and GetConVarString('sv_gamemodeoverride') != '') then
	MsgN( 'vgui_startnewgame_settings_gamemode = ' .. GetConVarString('sv_gamemodeoverride') )
	
	for sFileName , dPanelList in pairs( self.TabSheet.Settings.Gamemode.Groups ) do
		dPanelList:Clear()
		dPanelList:Remove()
		// dPanelList:SetTall( 0 )
	end
	self.TabSheet.Settings.Gamemode.Groups = {}
	self.TabSheet.Settings.Gamemode:Clear()
	
	for sFileName , tCvar in pairs( g_ds_menu.tSettings.Gamemode[ GetConVarString('sv_gamemodeoverride') or 'sandbox' ] or {} ) do
		if (!self.TabSheet.Settings.Gamemode.Groups[sFileName]) then self.TabSheet.Settings.Gamemode.Groups[sFileName] = vgui.Create("DPanelList" , self.TabSheet.Settings.Gamemode) end
		self.TabSheet.Settings.Gamemode.Groups[sFileName]:SetWide( self.TabSheet.Settings.Gamemode:GetWide() )
		self.TabSheet.Settings.Gamemode.Groups[sFileName]:SetTall( 22 )
		self.TabSheet.Settings.Gamemode.Groups[sFileName]:SetSpacing( 0 )
		self.TabSheet.Settings.Gamemode.Groups[sFileName]:EnableHorizontal( false )
		self.TabSheet.Settings.Gamemode.Groups[sFileName]:EnableVerticalScrollbar( true )
		self.TabSheet.Settings.Gamemode.Groups[sFileName].Paint = function()
			draw.RoundedBox( 3, 0, 0, self.TabSheet.Settings.Gamemode.Groups[sFileName]:GetWide() , self.TabSheet.Settings.Gamemode.Groups[sFileName]:GetTall(), Color( 102, 102, 102, 250 ) )
		end
		
		self.TabSheet.Settings.Gamemode.Groups[sFileName].Cvars = {}
		
		self.TabSheet.Settings.Gamemode.Groups[sFileName]:AddItem( getCvarPanel( 'dHeader' , {
			['type'] = 'header',
			['label'] = sFileName,
		} , self.TabSheet.Settings.Gamemode.Groups[sFileName] ) )
		
		local bOff = false
		for nIndex , tData in pairs( tCvar ) do
			local cvar_vgui = getCvarPanel( tData.cvar , tData , self.TabSheet.Settings.Gamemode.Groups[sFileName] , bOff )
			self.TabSheet.Settings.Gamemode.Groups[sFileName].Cvars[nIndex] = cvar_vgui
			self.TabSheet.Settings.Gamemode.Groups[sFileName]:AddItem( cvar_vgui )
			self.TabSheet.Settings.Gamemode.Groups[sFileName]:SetTall( self.TabSheet.Settings.Gamemode.Groups[sFileName]:GetTall() + cvar_vgui:GetTall() )
			bOff = !bOff
		end
		
		self.TabSheet.Settings.Gamemode:AddItem( self.TabSheet.Settings.Gamemode.Groups[sFileName] )
	end
	
	self.TabSheet.Settings.Gamemode.GameFolder = GetConVarString('sv_gamemodeoverride') or 'sandbox'
	
	end end)
	
--------------------------------------------------
	
	self.TabSheet.Settings.Basic.Tab = self.TabSheet.Settings:AddSheet( "Basic" , self.TabSheet.Settings.Basic , 'gui/silkicons/table_edit' )
	self.TabSheet.Settings.Advanced.Tab = self.TabSheet.Settings:AddSheet( "Advanced" , self.TabSheet.Settings.Advanced , 'gui/silkicons/table_edit' )
	self.TabSheet.Settings.Addons.Tab = self.TabSheet.Settings:AddSheet( "Addons" , self.TabSheet.Settings.Addons , 'gui/silkicons/table_edit' )
	self.TabSheet.Settings.Gamemode.Tab = self.TabSheet.Settings:AddSheet( "Gamemode" , self.TabSheet.Settings.Gamemode , 'gui/silkicons/table_edit' )
	
--------------------------------------------------
	
	if (!self.TabSheet.MapInfo) then self.TabSheet.MapInfo = vgui.Create( "DPanelList", self.TabSheet ) end
	self.TabSheet.MapInfo:SetPos( 0,0 )
	self.TabSheet.MapInfo:SetSize( self.TabSheet:GetWide(), self.TabSheet:GetTall() )
	self.TabSheet.MapInfo:SetSpacing( 0 )
	self.TabSheet.MapInfo:EnableHorizontal( false )
	self.TabSheet.MapInfo:EnableVerticalScrollbar( true )
	
--------------------------------------------------
	
	self.ToyBox = vgui.Create( "ToyboxMap", self )
	
--------------------------------------------------
	
	// garry broke tooltips?
	self.TabSheet:AddSheet( "Maps (Thumbnails)" , self.TabSheet.MapsT , 'gui/silkicons/application_view_tile' )
	self.TabSheet:AddSheet( "Maps (List)" , self.TabSheet.MapsL , 'gui/silkicons/application_view_detail' )
	self.TabSheet:AddSheet( "Settings" , self.TabSheet.Settings , 'gui/silkicons/table_edit' )
	self.TabSheet:AddSheet( "Map Information" , self.TabSheet.MapInfo , 'gui/silkicons/page' )
	self.TabSheet:AddSheet( "Toybox", self.ToyBox, "gui/silkicons/toybox" )
end

function PANEL:SetMap( sMap )
	local sGamemode = GetConVarString( 'sv_gamemodeoverride' )
	
	self:SetTitle( 'Start New Game (' .. sGamemode .. ' , ' .. sMap .. ')'  )
	
	self.Map = sMap
	g_SelectedMap = MapIcon
	
	self.ButtonStartSPGame:SetDisabled( false )
	self.ButtonStartMPGame:SetDisabled( false )
end

vgui.Register( "ds_menu_startnewgame", PANEL, "DFrame" )

--------------------------------------------------

function ds_menu_startnewgame_reload()
	g_ds_menu.startnewgame_vgui = vgui.Create( "ds_menu_startnewgame" )
	g_ds_menu.startnewgame_vgui:SetVisible( false )
	g_ds_menu.startnewgame_vgui:Center()
end

ds_menu_startnewgame_reload()

function ds_menu_startnewgame( pPlayer , sCommand , tArguments )
	
	if (!g_ds_menu.startnewgame_vgui ) then
		ds_menu_startnewgame_reload()
	else
		g_ds_menu.startnewgame_vgui:SetVisible( true )
		g_ds_menu.startnewgame_vgui:Recover()
		g_ds_menu.startnewgame_vgui:RequestFocus()
	end
	
end

concommand.Add( "menu_startgame", ds_menu_startnewgame )

function ds_menu_startnewgame_reload( pPlayer , sCommand , tArguments )
	
end

// concommand.Add( "menu_startgame_reload", ds_menu_startnewgame_reload )

function ds_menu_startnewgame_recover( pPlayer , sCommand , tArguments )
	g_ds_menu.startnewgame_vgui:Recover()
end

concommand.Add( "menu_startgame_recover", ds_menu_startnewgame_recover )

--------------------------------------------------

MsgN( 'Loaded "ds_menu/vgui_startnewgame.lua" in ' , ( math.Round( ( CurTime() - nStartTime ) * 10000 ) / 10000 ) , ' seconds' )

--------------------------------------------------
